//
//  SettingsViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/14.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "SettingsViewController.h"
#import "LoginAndRegisterViewController.h"
#import "SettingsTableViewCell.h"
#import "AppDelegate.h"
@interface SettingsViewController ()<UITableViewDataSource,UITableViewDelegate>

//建立tableView
@property(nonatomic,retain)UITableView *setTableView;
//建立一个装各个cell标题的数组
@property(nonatomic,retain)NSMutableArray *twoArray;
@property(nonatomic,retain)NSMutableArray *threeArray;


@end

@implementation SettingsViewController

-(void)dealloc{
    [_setTableView release];
    [_twoArray release];
    [_threeArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationItem.title = @"设置";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbar_back_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
 
    
    
    self.setTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.setTableView.backgroundColor = RGBAColor(239, 239, 244);
    [self.view addSubview:self.setTableView];
    [_setTableView release];
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    self.setTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.setTableView registerClass:[SettingsTableViewCell class] forCellReuseIdentifier:@"reuse"];
    //每个cell上的数组
    self.twoArray = [NSMutableArray arrayWithObjects:@"新消息通知",@"隐私",@"通用",nil];
    self.threeArray = [NSMutableArray arrayWithObjects:@"帮助与反馈",@"关于聊吧",nil];

    
    // Do any additional setup after loading the view.
}

-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark 分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
    
}

#pragma mark tableView显示分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;//(3个区)
    
}

#pragma mark tableView每个分区要显示的行数(cell的个数)必须实现的方法之一
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 3;
    }else if(section == 2){
    return 2;
    }
    return 1;
}


#pragma mark每个cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return ROOTHEIGHT / 15;
    
}


#pragma mark 必须实现的方法之2,作用是每显示一个cell都会执行这个方法.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    if (indexPath.section == 0) {
        cell.textLabel.text = @"账号与安全";
        //加箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    if (indexPath.section == 1) {
        cell.textLabel.text = self.twoArray[indexPath.row];
        if (indexPath.row == 0) {
            //[cell.contentView addSubview:self.lineLabel];
        }else if (indexPath.row == 1){
            
            //[cell.contentView addSubview:self.lineLabel];
        }
        
        //加箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }if (indexPath.section == 2) {
        cell.textLabel.text = self.threeArray[indexPath.row];
        if (indexPath.row == 0) {
            //[cell.contentView addSubview:self.lineLabel];
        }
        //加箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }if (indexPath.section == 3) {
        cell.textLabel.text = @"退出登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    
    
    return cell;
}




-(void)loginButton{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"退出后不会删除任何历史数据,下次登录依然可以使用本账号." preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//退出登录
        [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
            if (!error) {
                NSLog(@"退出成功");
               
                
            }
            
        } onQueue:dispatch_get_main_queue()];
        
//跳转回登录页面
        LoginAndRegisterViewController *loginVC = [[LoginAndRegisterViewController alloc]init];
//改为不是自动登录
        [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
        [self presentViewController:loginVC animated:YES completion:nil];
        [loginVC release];

//将添加好的单例清空
        [[AppDelegate shareFriendDic] removeAllObjects];
     
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}

#pragma mark cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 3) {
        
        [self loginButton];
        
    }else{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.color = [UIColor whiteColor];
        hud.labelColor = [UIColor blackColor];
        hud.labelText = @"程序猿正在日夜开发中";
        hud.dimBackground = YES;
    
    //    hud.activityIndicatorColor = [UIColor blackColor];
        [hud hide:YES afterDelay:1];
        
        
        
    }
    
    
}


//section跟tableView一起滚动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.setTableView)
    {
        CGFloat sectionHeaderHeight = 20;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y,0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
